package processus;
import interfaces.*;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.HashMap;


public class Processus_2 extends Thread{

	/**  */
    public String comServerName = "SERVER_COMM";
    
    public String transServerName = "sync1";
    
    public CommServerInterface comServer; 
    
    public SynchroServerInterface transServer;
    
    public String etatCourant;
    
    public HashMap<String,Integer> etatProcessus = new  HashMap<String,Integer> ();
    
    public String[] instances = {"P2_1"};
    
    public static int nbThreads = 1;
        
	public Processus_2(int rang) throws RemoteException, NotBoundException{
    	
        Registry registry = LocateRegistry.getRegistry("127.0.0.1");
    
        comServer= (CommServerInterface)registry.lookup("SERVER_COMM");
  
        transServer = (SynchroServerInterface)registry.lookup("sync");
        etatProcessus.put(new String("P2_1"),1);
        etatProcessus.put(new String("P2_2"),2);
        etatProcessus.put(new String("P2_3"),3);
        etatProcessus.put(new String("P2_4"),4);
        etatCourant = instances[rang];
	}
	
	public String method_P2_T4() throws InterruptedException{	
		System.out.println("Processus_2 : transition :" + etatCourant + " -------- P2_T4 --> P2_3" );
		Thread.sleep((int)Math.random()*10 %3 * 1000);
		return "P2_3";
	}
	
	public String method_P2_T3() throws InterruptedException,RemoteException{
		System.out.println("Processus_2 : transition :" + etatCourant + " -------- P2_T3 -->  P2_1" );
		Thread.sleep((int)Math.random()*10 %3 * 1000);
		return "P2_1";
	}
	
	public String method_P2_T1() throws RemoteException, InterruptedException{
		 boolean tr = comServer.consumeTokens("Mutex" ,1);
		 System.out.println("Processus_2 : demande au serveur de communication" );
		 Thread.sleep((int)Math.random()*10 %3 * 1000);
			if (tr) {
				System.out.println("Processus_2 : transition réussie :" + etatCourant + " --------P2_T1  --> P2_2" );
				return "P2_2";
			}
			else {
				System.out.println("Processus_2 : transition  échouée : " + etatCourant + " --------P2_T1  ----> " + etatCourant);
				return etatCourant;
			}
	}
	
	public String method_P2_T2() throws RemoteException, InterruptedException{
		System.out.println("Processus_2 : transition (comm_places):" + etatCourant + " -------- P2_T2 -->  P2_3" );
		Thread.sleep((int)Math.random()*10 %3 * 1000);
		comServer.produceTokens("Mutex" ,1);
		return "P2_3";
	}
	public String method_sync() throws RemoteException, InterruptedException{
		System.out.println("Processus_2 : demande au serveur de synchronisation" );
		boolean tr = transServer.synchronize();
		Thread.sleep((int)Math.random()*10 %3 * 1000);
		if (tr) {
			System.out.println("Processus_2 : transition (sync):" + etatCourant + " -------- sync --> P2_4" );
			return "P2_4";
		}
		else {
			System.out.println("Processus_2 : transition (je bouge pas):" + etatCourant + " -------- sync ----> " + etatCourant);
			return etatCourant;
		}
	}
		
		public void run(){
			
			while (true){
				switch(etatProcessus.get(etatCourant)){
				case 1:{
					int i = (int) ((Math.random() * 10) % 2) + 1;
					switch (i) {
					case 1 :{
						try {
							//System.out.println("Processus_2 : etat : " + etatCourant );
							etatCourant = method_P2_T1();
						} catch (RemoteException e) {
							System.err.println("Erreur lors de la tentative de commnication avec le gestionnaire des ressources ");
							e.printStackTrace();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						break;
					}
					case 2 :{
						//System.out.println("Processus_2 : etat : " + etatCourant );
						try {
							etatCourant = method_P2_T4();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						break;
					}
					}
					
					break;
				}
				case 2:{
					try {
						//System.out.println("Processus_2 : etat : " + etatCourant );
						etatCourant = method_P2_T2();
					} catch (RemoteException e) {
						System.err.println("Erreur lors de la tentative de commnication avec le gestionnaire des ressources ");
						e.printStackTrace();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				}
				case 3:{
					try {
						//System.out.println("Processus_2 : etat : " + etatCourant );
						etatCourant = method_sync();
					} catch (RemoteException e) {
						System.err.println("Erreur lors de la tentative de commnication avec le gestionnaire de la synchro");
						e.printStackTrace();
					} catch (InterruptedException e) {
						System.err.println("Erreur dans la demande desynchronisation");
						e.printStackTrace();
					}
					break;
				}
				case 4:{
					//System.out.println("Processus_2 : etat : " + etatCourant );
					try {
						etatCourant = method_P2_T3();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				}
				}
			}
		}
	}

